
#include <BEntry>
#include <BFolder>
#include <BReadWrite>
#include <BIcon>
#include "member_BIcon.h"

using namespace BWE;

#define member					(*(member_BIcon*)_ptr)
#define member_allocate()		_ptr = new member_BIcon(this)
#define member_release()		delete (member_BIcon*)_ptr

BIcon::BIcon()
{
	member_allocate();
}
BIcon::BIcon(BImage* image)
{
	member_allocate();
	member.images[State_Normal] = image;
}
BIcon::BIcon(const BString& fileName)
{
	member_allocate();
	if (fileName.ext())
	{
		BImageHolder image = Read<BImage>(fileName);
		setImage(image);
	}
	else
	{
		if (BFolder::Exists(fileName))
		{
			return;
		}
		else
		{
			BString name = fileName.name();
			name += '-';
			BFolder folder(fileName.path());
			if (folder.open())
			{
				for (int i = 0; i < folder.entryCount(); i++)
				{
					BEntry* entry = folder.entry(i);
					const BString& entryName = entry->name();
					if (entry->name().beginsWidth(name) && entry->name().ext())
					{
						if (member.images[State_Normal] == 0 && entry->name().contains("-normal."))
						{
							member.images[State_Normal] = Read<BImage>(folder.path() + '/' + entry->name());
							continue;
						}
						if (member.images[State_Hovered] == 0 && entry->name().contains("-hover."))
						{
							member.images[State_Hovered] = Read<BImage>(folder.path() + '/' + entry->name());
							continue;
						}
						if (member.images[State_Pressed] == 0 && entry->name().contains("-pressed."))
						{
							member.images[State_Pressed] = Read<BImage>(folder.path() + '/' + entry->name());
							continue;
						}
						if (member.images[State_Checked] == 0 && entry->name().contains("-checked."))
						{
							member.images[State_Checked] = Read<BImage>(folder.path() + '/' + entry->name());
						}
						if (member.images[State_Disable] == 0 && entry->name().contains("-disable"))
						{
							member.images[State_Disable] = Read<BImage>(folder.path() + '/' + entry->name());
							continue;
						}
					}
				}
			}
		}
	}
}
BIcon::BIcon(const BIcon& other)
{
	member_allocate();
	member.images[State_Normal] = icon_member(&other)->images[State_Normal];
	member.images[State_Hovered] = icon_member(&other)->images[State_Hovered];
	member.images[State_Pressed] = icon_member(&other)->images[State_Pressed];
	member.images[State_Checked] = icon_member(&other)->images[State_Checked];
	member.images[State_Disable] = icon_member(&other)->images[State_Disable];
}
BIcon::~BIcon()
{
	member_release();
}

void BIcon::setImage(BImage* image, State state)
{
	if (state >= 0 && state < State_Max)
	{
		member.images[state] = image;
	}
}
void BIcon::setImage(const BString& imageFileName, State state)
{
	if (imageFileName.size() && state >= 0 && state < State_Max)
	{
		BImage* image = Read<BImage>(imageFileName);
		member.images[state] = image;
	}
}

const BImage* BIcon::image(State state) const
{
	if (state >= 0 && state < State_Max)
	{
		if (member.images[state] == 0)
			member.generateImageForState(state);
		return member.images[state];
	}
	return 0;
}
BImage* BIcon::image(State state)
{
	if (state >= 0 && state < State_Max)
	{
		if (member.images[state] == 0)
			member.generateImageForState(state);
		return member.images[state];
	}
	return 0;
}

bool BIcon::empty() const
{
	for (int i = 0; i < State_Max; i++)
	{
		if (member.images[i].ptr())
			return false;
	}
	return true;
}

